package com.spring.gateway.snail.filter;

import org.apache.commons.lang3.StringUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Date;

/**
 * @Description TODO
 * @Author xueguoping
 * @Date 2021/5/12 11:29
 */
@Component
public class MyLogGateWayFilter implements GlobalFilter,Ordered {
	@Override
	public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
		System.out.println("***********************come in MyLogGateWayFilter: "+new Date());
		String uname = exchange.getRequest().getQueryParams().getFirst("uname");
		if(StringUtils.isBlank(uname)){
			System.out.println("***********************uname is null ");
			exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE);
			return exchange.getResponse().setComplete();
		}
		return chain.filter(exchange);
	}

	/**
	 * 加载过滤器的顺序  数字越小 优先级越高
	 * @return
	 */
	@Override
	public int getOrder() {
		return 0;
	}
}
